package hcQuest;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.Date;

import webExperiment.Database;
import webExperiment.SQLHandler;
import webExperiment.usefull;

class HCQ_Database extends Database {

    final static String newParticipant = "newHCQParticipant.php";
    final static String saveData = "saveHCQData.php";
    final static String sendMail = "sendData.php";
    final static String phpIdxVarName = "pIdx";
    static String PHPPATH = "";
    public boolean sqlOK = false;

    public HCQ_Database(short DBMode, String ExperimentTag,
	    String AppletVersion, URL SQLiniFile, URL SQLfile) {
	super(ExperimentTag, AppletVersion);
	if (DBMode == Database.PHP_MODE) {
	    Database.PHPDetails de = new Database.PHPDetails(PHPPATH,
		    newParticipant, saveData, sendMail);
	    setPHPMode(de, phpIdxVarName);
	} else if (DBMode == Database.SQL_MODE) {
	    String tab = "HCQ_data";
	    SQLHandler.ConnectionDetails sqlDetails = new SQLHandler.ConnectionDetails(
		    "", "", "", "");
	    sqlDetails.password = "oliver";
	    sqlDetails.server = "localhost";
	    sqlDetails.database = "data";
	    sqlDetails.user = "Oliver";
	    if (SQLiniFile != null) {
		try {
		    sqlDetails.loadFromFile(SQLiniFile);
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	    sqlOK = setSQLMode(sqlDetails, tab, "pIdx");
	    if (sqlOK) {
		try {
		    ensureTableExist(SQLfile);
		} catch (SQLException e) {
		    e.printStackTrace();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	} else {
	    setFileMode();
	}

    }

    public int newParticipant() throws SQLException {
	// systeminfo OS, screen size etc.
	Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
	int res = Toolkit.getDefaultToolkit().getScreenResolution();
	String OS = System.getProperty("os.name") + ", "
		+ System.getProperty("os.version") + ", s:[" + size.width + ","
		+ size.height + "], r:" + res + "";
	String data = "";
	Date now = new Date();
	data = data + "&" + "exp=" + removeSpecialChars(getTag());
	data = data + "&" + "applet=" + removeSpecialChars(getAppletName());
	data = data + "&" + "java="
		+ removeSpecialChars(usefull.getJavaVersion());
	data = data + "&" + "os=" + removeSpecialChars(OS);
	data = data + "&" + "date="
		+ removeSpecialChars(SQLHandler.sqlDate(now));
	data = data + "&" + "startTime="
		+ removeSpecialChars(SQLHandler.sqlTime(now));
	return newEntry(data);
    }

    public String saveHCQData(String fngSq, String times, String answers)
	    throws SQLException { // end of experiment
	String data = "";
	data = data + "&" + "fngSq=" + removeSpecialChars(fngSq);
	data = data + "&" + "times=" + removeSpecialChars(times);
	data = data + "&" + "answers=" + removeSpecialChars(answers);
	return addData(data);
    }

    public String saveHCQPersonData(String language, String initials,
	    short gender, String year, String mtongue, String countryOfBirth)
	    throws SQLException {
	String data = "";
	data = data + "&" + "initials=" + removeSpecialChars(initials);
	data = data + "&" + "gender=" + gender;
	data = data + "&" + "year=" + removeSpecialChars(year);
	data = data + "&" + "mtongue=" + removeSpecialChars(mtongue);
	data = data + "&" + "lang=" + removeSpecialChars(language);
	data = data + "&" + "country=" + removeSpecialChars(countryOfBirth);
	return addData(data);
    }

    public String writeEndTime() throws SQLException {
	Date now = new Date();
	String data = "";
	data = data + "&" + "endTime="
		+ removeSpecialChars(SQLHandler.sqlTime(now));
	String rtn = addData(data);
	saveDataLineToDisk();
	return rtn;
    }

}
